/*
 * Expression.cpp
 *
 *  Created on: Aug 7, 2011
 *      Author: dino
 */

#include "Expression.h"
#include <stdlib.h>

template<class T>
void deleteVec(vector<T>& vec) {
	for(int i = 0; i < (int)vec.size(); i++) {
		delete(vec[i]);
	}
	vec.clear();
}

Expression::Expression() {
	parent = NULL;
}

Expression::~Expression() {
	deleteVec<Expression*>(operands);
}
void Expression::addOperand(Expression* operand) {
	operand->parent = this;
	operands.push_back(operand);
}

Expression* Expression::getOperand(int index) {
	return operands[index];
}

void Expression::replaceOperand(Expression* original, Expression* replacement) {
	for(int i = 0; i < (int)operands.size(); i++) {
		if (operands[i] == original) {
			replacement->parent = this;
			operands[i] = replacement;
			delete original;
			return;
		}
	}
}

bool Expression::simplifyOperands() {
	bool simplified = false;
	for(int i = 0; i < (int)operands.size(); i++) {
		simplified != operands[i]->simplify();
	}
	return simplified;
}


